package com.example.jpetstoreclient.controller;

import com.example.jpetstoreclient.common.CommonResponse;
import com.example.jpetstoreclient.domain.Address;
import com.example.jpetstoreclient.service.AddressService;
import jakarta.validation.constraints.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Validated
@CrossOrigin
@RestController
@RequestMapping("/address")
public class AddressController {
    @Autowired
    private AddressService addressService;

    //  添加地址
    @PostMapping("/save")
    public CommonResponse<Address> save(@RequestParam("id") @NotNull(message = "用户不能为空") int user_id,
                                        @RequestParam("address") String address){
        return addressService.addAddress(user_id, address);
    }

    //  查找地址
    @GetMapping("/{id}")
    public CommonResponse<List<Address>> getAll(@PathVariable("id") int user_id){
        return addressService.getAllAddresses(user_id);
    }

    //  更新地址
    @PutMapping("/update")
    public CommonResponse<Address> update(@RequestBody Address address){
        return addressService.updateAddress(address);
    }

    //  删除地址
    @DeleteMapping("/delete/{id}")
    public CommonResponse<Address> delete(@PathVariable int id){
        return addressService.deleteAddress(id);
    }
}
